/**
 *
 * 2007-11-4 下午03:02:44
 * @author xuhuan
 */

package edu.ccut.saturn.studio.action;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.views.properties.IPropertySheetPage;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.w3c.dom.Document;

import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.logic.contextaction.IEditorMultiPage;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.ShapesDiagram;
import edu.ccut.saturn.studio.logic.model.commands.FakeCommand;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnActionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnTransformNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnViewNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextNode;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnDecisionNodeInAction;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnInformationNodeInAction;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnLogicInAction;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnStartNodeInAction;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnDecisionNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnInformationNodeInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnLogicInLogic;
import edu.ccut.saturn.studio.logic.model.saturn.logic.SaturnStartNodeInLogic;
import edu.ccut.saturn.studio.logic.model.xml.ModelXmlUtils;
import edu.ccut.saturn.studio.logic.model.xml.loader.ActionNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.DataLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.NodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.TranformNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.ViewLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.WebContextNodeLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.action.LogicNodeInActionLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.action.SaturnDecisionNodeInActionLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.action.SaturnInformationInActionLoader;
import edu.ccut.saturn.studio.logic.model.xml.loader.action.SaturnStartNodeInActionLoader;

public class ActionMultiPageEditor extends MultiPageEditorPart implements
		IResourceChangeListener, ITabbedPropertySheetPageContributor,
		IEditorMultiPage {

	private ActionEditor actionEditor;

	private StructuredTextEditor textEditor;

	private int actionEditorIndex = 0;

	private int actiontextEditorIndex = 0;

	private Document document;
	private List<Class<?>> child = new ArrayList<Class<?>>();

	public StructuredTextEditor getTextEditor() {
		return textEditor;
	}

	private void createGraphicalEditPage() {

		try {
			List<Shape> shapes = ModelXmlUtils.loadModels(this.document,
					new NodeLoader[] { new SaturnInformationInActionLoader(),
							new SaturnStartNodeInActionLoader(),
							new LogicNodeInActionLoader(), new ViewLoader(),
							new DataLoader(), new ActionNodeLoader(),
							new SaturnDecisionNodeInActionLoader(),
							new TranformNodeLoader(),
							new WebContextNodeLoader() });

			this.actionEditor = new ActionEditor(shapes, this.document);

			this.actionEditorIndex = addPage(actionEditor, getEditorInput());
			setPageText(this.actionEditorIndex, "action");
			setTitle(getEditorInput().getName());
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private void createSourcePage() {
		try {
			textEditor = new StructuredTextEditor();
			this.actiontextEditorIndex = addPage(textEditor, getEditorInput());

			setPageText(this.actiontextEditorIndex, "source");
			this.document = getDocument(textEditor);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	private Document getDocument(StructuredTextEditor source) {
		Document document = (Document) source.getAdapter(Document.class);
		if (document.getDocumentElement() == null) {
			SaturnProjectUtil.refreshProject(getEditorInput());
		}
		return document;
	}

	protected void createPages() {
		createSourcePage();
		createGraphicalEditPage();
		this.setActivePage(this.actionEditorIndex);
		this.child.add(SaturnStartNodeInAction.class);
		this.child.add(SaturnInformationNodeInAction.class);
		this.child.add(SaturnActionNode.class);
		this.child.add(SaturnLogicInAction.class);
		this.child.add(SaturnDecisionNodeInAction.class);
		this.child.add(SaturnViewNode.class);
		this.child.add(SaturnDataNode.class);
		this.child.add(SaturnTransformNode.class);
		this.child.add(SaturnStartNodeInLogic.class);
		this.child.add(SaturnInformationNodeInLogic.class);
		this.child.add(SaturnLogicInLogic.class);
		this.child.add(SaturnDecisionNodeInLogic.class);
		this.child.add(SaturnWebContextNode.class);
		// String fileName = "";
		// List<Shape> list = actionEditor.getDiagram().getShapes();
		// for(Shape shape : list){
		// if(shape instanceof SaturnInformationNode){
		// if(shape.getKey().indexOf("/")!=-1){
		// fileName =
		// shape.getKey().substring(shape.getKey().lastIndexOf("/"),shape.getKey().length());
		// }
		// }
		// }
		// Map map = SingleUitl.getSingleUitl().getDebugMap();
		// SaturnDebug deubg = (SaturnDebug)map.get(fileName);
		// if(deubg!=null){
		// boolean mode = deubg.isDebugMode();
		// if(mode){
		// for(Shape shape : list){
		// if(shape instanceof SaturnStartNode){
		// SubstituteIcon sub = new SubstituteIcon(shape);
		// sub.exectute();
		// }
		// }
		// }
		// }
	}

	public void doSave(IProgressMonitor monitor) {
		ModelXmlUtils.refreshModelXml(actionEditor.getDiagram());
		this.actionEditor.doSave(monitor);
		textEditor.doSave(monitor);

	}

	public void doSaveAs() {

	}

	public boolean isSaveAsAllowed() {
		return false;
	}

	public void resourceChanged(IResourceChangeEvent event) {

	}

	protected void pageChange(int newPageIndex) {
		if (newPageIndex == this.actiontextEditorIndex) {
			ModelXmlUtils.refreshModelXml(actionEditor.getDiagram());
		}

		super.pageChange(newPageIndex);
	}

	public Object getAdapter(Class adapter) {
		if (adapter == IPropertySheetPage.class) {
			return new TabbedPropertySheetPage(this);
		}
		return super.getAdapter(adapter);
	}

	public String getContributorId() {
		return getSite().getId();
	}

	public ActionEditor getActionEditor() {
		return actionEditor;
	}

	public void setActionEditor(ActionEditor actionEditor) {
		this.actionEditor = actionEditor;
	}

	public ShapesDiagram getDiagram() {
		return this.actionEditor.getDiagram();
	}

	public Point getMousePoint() {
		return this.actionEditor.getMousePoint();
	}

	public boolean hasChild(Class<?> shape) {
		for (Class<?> c : this.child) {
			if (c == shape) {
				return true;
			}
		}
		return false;
	}

	public void fakesaveimpl() {
		this.getDiagram().getEditPart().getViewer().getEditDomain()
				.getCommandStack().execute(new FakeCommand());
	}
}
